package com.redwood.report.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.redwood.report.R;
import com.redwood.report.common.Config;
import com.redwood.report.common.Constant;
import com.redwood.report.event.CurrentNowEvent;
import com.redwood.report.event.RedChangeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 报修列表
 */
public class BaoXiuFragment extends BaseLazyFragment {


    @BindView(R.id.rb_weichuli)
    RadioButton rbWeichuli;
    @BindView(R.id.rb_chulizhong)
    RadioButton rbChulizhong;
    @BindView(R.id.rb_yichuli)
    RadioButton rbYiChuli;
    @BindView(R.id.operate_rg)
    RadioGroup operateRg;
    @BindView(R.id.vp_main_baoxiu)
    ViewPager vpMainBaoXiu;
    @BindView(R.id.rb_huishouxiang)
    RadioButton rbHuishouxiang;
    @BindView(R.id.red_weichuli_tv)
    TextView redWeichuliTv;
    @BindView(R.id.red_chulizhong_tv)
    TextView redChulizhongTv;
    @BindView(R.id.four_fl)
    FrameLayout fourFl;
    private int viewpageSize = 3;
    int checkPreId = R.id.rb_weichuli;
    private WeiChuliFragment yiChuliFragment;
    private WeiChuliFragment chuliZhongFragment;
    private WeiChuliFragment weiChuliFragment;
    private HuiShouXiangFragment huiShouXiangFragment;

    /**
     * 布局文件ID（重载）
     *
     * @return
     */
    protected int getLayoutId() {
        return R.layout.fragment_baoxiu;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedChangeEvent(RedChangeEvent event) {
        Log.i("supeng",event.count+"-"+event.doingType);
        readRedCount(event.count,event.doingType);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurrentNowEvent(CurrentNowEvent event) {
        vpMainBaoXiu.setCurrentItem(event.page);
    }

    private void readRedCount(int count,String mdoingType) {

        if(mdoingType.equals(Constant.WEICHULI)){
            redWeichuliTv.setVisibility(View.VISIBLE);
            int redWeiChuliCount=count;
            //redWeiChuliCount=count-DataBaseHelper.getInstance().getRedCount(true, Config.getUserInfo().getUserid());
            redWeichuliTv.setText(redWeiChuliCount+"");

            if(redWeiChuliCount<=0){
                redWeichuliTv.setVisibility(View.GONE);
            }
            if(redWeiChuliCount>99){
                redWeichuliTv.setText("99+");
            }
        }

        if(mdoingType.equals(Constant.ChuLIZHONG)){
            redChulizhongTv.setVisibility(View.VISIBLE);
            int redChuliZhongCount=count;
          //  redChuliZhongCount= count-DataBaseHelper.getInstance().getRedCount(false,Config.getUserInfo().getUserid());
            redChulizhongTv.setText(redChuliZhongCount+"");
            if(redChuliZhongCount>99){
                redChulizhongTv.setText("99+");
            }
            if(redChuliZhongCount<=0){
                redChulizhongTv.setVisibility(View.GONE);
            }
        }



    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 初始化组件（重载）
     */
    protected void initViewData(View entryView) {
        //管理员
        if (Config.getUserInfo().getRole().equals(Constant.GUANLIYUAN)) {
            viewpageSize=4;
            vpMainBaoXiu.setOffscreenPageLimit(viewpageSize);
        } else {
            viewpageSize=3;
            vpMainBaoXiu.setOffscreenPageLimit(viewpageSize);
            rbHuishouxiang.setVisibility(View.GONE);
            fourFl.setVisibility(View.GONE);


        }
        vpMainBaoXiu.setAdapter(new PagerAdapter(getFragmentManager()));
        vpMainBaoXiu.setOnPageChangeListener(new PageChangeListener());
        operateRg.setOnCheckedChangeListener(new MenuCheckedChangeListener());
    }



    private class MenuCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_weichuli:
                    vpMainBaoXiu.setCurrentItem(0);
                    break;
                case R.id.rb_chulizhong:
                    vpMainBaoXiu.setCurrentItem(1);
                    break;
                case R.id.rb_yichuli:
                    vpMainBaoXiu.setCurrentItem(2);
                    break;
                case R.id.rb_huishouxiang:
                    vpMainBaoXiu.setCurrentItem(3);
                    break;
            }
        }
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            checkPreId = operateRg.getCheckedRadioButtonId();
            switch (position) {
                case 0:
                    operateRg.check(R.id.rb_weichuli);
                    break;
                case 1:
                    operateRg.check(R.id.rb_chulizhong);
                    break;
                case 2:
                    operateRg.check(R.id.rb_yichuli);
                    break;
                case 3:
                    operateRg.check(R.id.rb_huishouxiang);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
    }


    private class PagerAdapter extends FragmentPagerAdapter {
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                if (weiChuliFragment == null) {
                    weiChuliFragment = WeiChuliFragment.newInstance(Constant.WEICHULI, Config.getUserInfo().getRole());
                }
                return weiChuliFragment;
            } else if (position == 1) {
                if (chuliZhongFragment == null) {
                    chuliZhongFragment = WeiChuliFragment.newInstance(Constant.ChuLIZHONG, Config.getUserInfo().getRole());
                }
                return chuliZhongFragment;
            } else if (position == 2) {
                if (yiChuliFragment == null) {
                    yiChuliFragment = WeiChuliFragment.newInstance(Constant.YICHULI, Config.getUserInfo().getRole());
                }
                return yiChuliFragment;
            } else {
                if (huiShouXiangFragment == null) {
                    huiShouXiangFragment = new HuiShouXiangFragment();
                }
                return huiShouXiangFragment;
            }
        }

        @Override
        public int getCount() {
            return viewpageSize;
        }
    }

    @Override
    protected void managerArguments() {

    }

    /**
     * 初始化数据,必须重载（重载）
     */
    protected void initData() {
        loaded();
    }

    /**
     * 第一次页面呈现的时候调用（重载）
     */
    protected void onFirstUserVisible() {
        //初始化数据
    }

    /**
     * 非第一次页面呈现的时候调用（重载）
     */
    protected void onUserVisible() {

    }

    /**
     * 第一次页面消失的时候调用（重载）
     */
    protected void onFirstUserInvisible() {
    }

    /**
     * 非第一次页面消失的时候调用（重载）
     */
    protected void onUserInvisible() {

    }


}
